<?php

// -------------------------------------------------------------
//  ----  Functions  -------------------------------------------
// -------------------------------------------------------------
function helloWorld() {
	echo 'Hello world!';
}
// -------------------------------------------------------------
function getTierItemArray($tier)
{
	/*$tierItemArray = array(
							40625 => 'Breastplate of the Lost Conqueror';
							40626 => 'Breastplate of the Lost Protector';
							40627 => 'Breastplate of the Lost Vanquisher';
							40628 => 'Gauntlets of the Lost Conqueror';
							40629 => 'Gauntlets of the Lost Protector';
							40630 => 'Gauntlets of the Lost Vanquisher';
							40631 => 'Crown of the Lost Conqueror';
							40632 => 'Crown of the Lost Protector';
							40633 => 'Crown of the Lost Vanquisher';
							40634 => 'Legplates of the Lost Conqueror';
							40635 => 'Legplates of the Lost Protector';
							40636 => 'Legplates of the Lost Vanquisher';
							40637 => 'Mantle of the Lost Conqueror';
							40638 => 'Mantle of the Lost Protector';
							40639 => 'Mantle of the Lost Vanquisher';
						);*/
	if ($tier == 75) {
		$tierItemArray = array(	40625,
								40626,
								40627,
								40628,
								40629,
								40630,
								40631,
								40632,
								40633,
								40634,
								40635,
								40636,
								40637,
								40638,
								40639
							);
	}
	return $tierItemArray;
}
// -------------------------------------------------------------
function getRaiderDisplayArray()
{
	global $db;
	$raiderDisplayArray = array();
	$result = $db->query('
	select
		m.ClassID as classID,
		m.RoleID as roleID,
		m.MemberID,
		c.name as className,
		c.colour as classColour,
		c.image as classImage,
		r.RoleName as roleName,
		r.image as roleImage,
		m.Name
	from
		dmzdkp_members m,
		dmzdkp_classes c,
		dmzdkp_roles r
	where
		c.ClassID = m.ClassID
	and	r.RoleID = m.RoleID
		', true) or error('Unable to fetch raider data (getRaiderDisplayArray)', __FILE__, __LINE__, $db->error());

	while ($cur_row = $db->fetch_assoc($result))
	{
		$raiderDisplayArray[$cur_row['MemberID']] = array(
			'classID' => $cur_row['classID'],
			'className' => $cur_row['className'],
			'classColour' => $cur_row['classColour'],
			'classImage' => $cur_row['classImage'],
			'roleID' => $cur_row['roleID'],
			'roleName' => $cur_row['roleName'],
			'roleImage' => $cur_row['roleImage'],
			'MemberID' => $cur_row['MemberID'],
			'userName' => $cur_row['Name'],
		);
	}
	return $raiderDisplayArray;
}
// -------------------------------------------------------------
function sortArray(&$a) {
	ksort($a);
	foreach($a as &$value) {
		if (is_array($value))
			sortArray($value);
	}
	return $a;
}
// -------------------------------------------------------------
function displayRaider($id, $roleImage, $roleName, $classImage, $className, $classColour, $name, $lineEnd, $titleText="", $fontWeight="bold")
{
	$roleImage = '<img src="'.$roleImage.'" height="15px" width="15px" title="'.$roleName.'" />&nbsp;';
	$classImage = '<img src="./img/clandmz/'.$classImage.'" height="15px" width="15px" title="'.$className.'" />&nbsp;&nbsp;';

	echo $roleImage.$classImage.'<a href="index.php?page=dkptables&m='.$id.'" title="'.$titleText.'" style="color: #'.$classColour.'; font-weight: '.$fontWeight.'; text-decoration: none">'.$name.'</a>'.$lineEnd;
}
// -------------------------------------------------------------
function addLogEntry($class, $code, $decode, $value, $reference, $title, $author, $authoriser, $start_date, $end_date)
{
	if ($class == '')
		$class = 'dkpLunknwn';
	if ($code == '')
		$code = 0;
	if ($decode == '')
		$decode = 'Unknown entry';
	if ($value == '')
		$value = 0;
	if ($reference == '')
		$reference = 0;
	if (title == '')
		$title = 0;
	if (author == '')
		$author = 0;
	if (authoriser == '')
		$authoriser = 0;
	if (start_date == '')
		$start_date = 0;
	if (end_date == '')
		$end_date = 0;
 	if ($start_date == 'now')
		$start_date = time();
	if ($end_date == 'now')
		$end_date = time();
 	global $db, $pun_user;
	$db->query("
			INSERT INTO
				clandmz_site_content (class, code, decode, value, reference, title, author, authoriser, start_date, end_date)
			VALUES
				('".$class."', '".$code."', '".$decode."', ".$value.", ".$reference.", '".$title."', '".$author."', '".$authoriser."', '".$start_date."', '".$end_date."')
		")
		or error('Unable to add log entry', __FILE__, __LINE__, $db->error());
}
// -------------------------------------------------------------
function isEditable()
/*
Determines whether the current logged on user has permission to update
*/
{
    global $pun_user;
	/*--groups--
	1 Administrators
	2 Officers (modorators)
	3 Guest
	4 Forum members
	6 Members
	*/
	if ($pun_user['g_id'] == 1 || $pun_user['g_id'] == 2 || $pun_user['id'] == 3)
	{
		return true;
	} else {
		return false;
	}
}
// -------------------------------------------------------------
function showRaidList($raidArray, $raidID)
{
	echo '<table cellspacing="0" style="border-bottom: 1px solid #CCC;">';
	foreach ($raidArray as $raid)
	{
		if ($raid[hidden] == 0 || isEditable()) {
			$isCurrentRow = false;
			$defaultRowColour = 'f1f1f1';
			if ($raid[hidden] == 1)
				$defaultRowColour = 'e49d9d';
			if ($raid[RaidID] == $raidID && !isset($_GET['action']))
			{
				$isCurrentRow = true;
				$defaultRowColour = 'ccb';
			}
			showRaidRow($raid[RaidID], $raid[Date], $raid[DungeonName], $raid[DungeonTier], $defaultRowColour, $isCurrentRow, $raid[wws_url], $raid[BonusValue], round($raid[RaidValue],0));
		}
	}
	echo '</table>';
}
// -------------------------------------------------------------
function showRaidRow($raidID, $raidDate, $dungeonName, $dungeonTier, $defaultRowColour, $isCurrentRow, $wwsURL, $bonusValue, $raidDKPValue)
{
// ***** If any cols are added the colspan in showRaiderDetails() will need adjusting *****
	?>
		<tr bgcolor="#<?php echo $defaultRowColour; ?>" onMouseOver="this.bgColor='#ccb';" onMouseOut="this.bgColor='#<?php echo $defaultRowColour; ?>';">
			<td width="17px" style="padding: 3px 4px 1px 4px; margin: 0px 0px 0px 0px; border-top: 1px solid #CCC;">
				<?php echo '<img src="./site/dkp/images/instances/tier'.$dungeonTier.'.gif" title="Tier '.$dungeonTier.'"/>'; ?>
			</td>
			<td style="padding: 1px 4px 1px 4px; margin: 0px 0px 0px 0px; border-top: 1px solid #CCC;">
				<?php
					if ($isCurrentRow)
					{
						echo $dungeonName;
					} else {
						echo '<a href="index.php?page=dkpraids&r='.$raidID.'">'.$dungeonName.'</a>';
					}
				?>
			</td>
			<td width="20px" style="padding: 3px 4px 1px 4px; margin: 0px 0px 0px 0px; border-top: 1px solid #CCC;">
				<?php
					if (isEditable()) {
						echo '<a href="./index.php?page=dkpraids&action=update&r='.$raidID.'" title="Edit raid"><img src="./images/common/icons/cog.png" /></a>';
					}
				?>
			</td>
			<td width="25px" style="padding: 3px 4px 1px 4px; margin: 0px 0px 0px 0px; border-top: 1px solid #CCC; text-align: right; background-image:url(./site/dkp/images/dkp_faded_16.png); background-repeat:no-repeat; background-position:center;">
				<?php
					echo '<span style="font-weight: bold; font-size: 8px">'.$raidDKPValue.'</span>';
				?>
			</td>
			<td width="20px" style="padding: 3px 4px 1px 4px; margin: 0px 0px 0px 0px; border-top: 1px solid #CCC; text-align: right; background-image:url(./site/dkp/images/bonus_faded_16.png); background-repeat:no-repeat; background-position:center;">
				<?php
					echo '<span style="font-weight: bold; font-size: 8px">'.$bonusValue.'</span>';
				?>
			</td>
			<td width="20px" style="padding: 3px 4px 1px 4px; margin: 0px 0px 0px 0px; border-top: 1px solid #CCC;">
				<?php
					if (strlen($wwsURL) > 0) {
						echo '<a href="'.$wwsURL.'" title="View the stats for this raid." target="_BLANK"><img src="./images/common/icons/chart_curve.png" /></a>';
					}
				?>
			</td>
			<td width="120px" style="padding: 3px 4px 1px 4px; margin: 0px 0px 0px 0px; border-top: 1px solid #CCC;">
				<?php echo '<img src="./site/dkp/images/'.date('l', strtotime($raidDate)).'.gif" title="'.date('l', strtotime($raidDate)).'"/>'; ?>
			</td>
			<td width="110px" style="padding: 1px 4px 1px 4px; margin: 0px 0px 0px 0px; border-top: 1px solid #CCC; text-align: right;">
				<?php echo date('j F \'y', strtotime($raidDate)); ?>
			</td>
		</tr>
	<?php
}
// -------------------------------------------------------------
function getLastRaidID($raidArray)
{
	foreach ($raidArray as $raid)
	{
		$lastRaidID = $raid[RaidID];
		if ($raid[hidden] == 0)
			break;
	}
	return $lastRaidID;
}
// -------------------------------------------------------------
function setHidden($setting)
{
   	global $db, $pun_user;
	$db->query("
			UPDATE
				dmzdkp_raids
			SET
				hidden = ".$setting."
			WHERE
				RaidID = ".$_GET['r'])
		or error('Unable to update raid visibility', __FILE__, __LINE__, $db->error());
//	function addLogEntry($class, $code, $decode, $value, $reference, $title, $author, $authoriser, $start_date, $end_date)
//	addLogEntry('dkpLEdtRdr', $oldRaiderClass, $editRaiderName, $editRaiderClass, $editRaiderRole, $oldRaiderName, $pun_user['username'], $oldRaiderRole, 'now', '');
}
// -------------------------------------------------------------
function setWWSURL($setting)
{
//	echo $setting;
   	global $db, $pun_user;
	$db->query("
			UPDATE
				dmzdkp_raids
			SET
				wws_url = '".$setting."'
			WHERE
				RaidID = ".$_GET['r'])
		or error('Unable to update WWS URL', __FILE__, __LINE__, $db->error());
//	function addLogEntry($class, $code, $decode, $value, $reference, $title, $author, $authoriser, $start_date, $end_date)
//	addLogEntry('dkpLEdtRdr', $oldRaiderClass, $editRaiderName, $editRaiderClass, $editRaiderRole, $oldRaiderName, $pun_user['username'], $oldRaiderRole, 'now', '');
}
// -------------------------------------------------------------
function showEditRaidDetail($raidDetailArray, $addedRaiders, $signupArray)
{
	echo '<div style="margin: 2px 4px 4px 4px; padding: 5px 0px 8px 0px; border-top: 1px solid #CCC;"></div>';
	echo '<form method="post" action="index.php?page=dkpraids&action=update&r='.$_GET['r'].'" name="setBonus">Bonus for raid: <input type="text" name="setBonus">&nbsp;<input type="submit" value="Set raid bonus"></form><br />';
	echo '<form method="post" action="index.php?page=dkpraids&action=update&r='.$_GET['r'].'" name="wwsURL">Full stats report URL: <input type="text" name="wwsURL">&nbsp;<input type="submit" value="Set report URL"></form><br />';
	echo '<div style="margin: 2px 4px 4px 4px; padding: 5px 0px 8px 0px; border-top: 1px solid #CCC;"></div>';
	echo 'Select Raiders';
	$raidersArray = getRaidersArray();
	displayActiveRaiders($raidersArray, $addedRaiders, $signupArray);
	echo '<div style="margin: 2px 4px 4px 4px; padding: 5px 0px 8px 0px; border-top: 1px solid #CCC;"></div>';
	echo '<p><a name="selectDrops">Select drops</a></p><br />';
	showSelectDrops($raidDetailArray);
}
// -------------------------------------------------------------
function setBonus($bonusValue)
{
   	global $db, $pun_user;
	$db->query("
			UPDATE
				dmzdkp_raids
			SET
				BonusValue = '".$bonusValue."'
			WHERE
				RaidID = ".$_GET['r'])
		or error('Unable to update bonus', __FILE__, __LINE__, $db->error());
//	function addLogEntry($class, $code, $decode, $value, $reference, $title, $author, $authoriser, $start_date, $end_date)
//	addLogEntry('dkpLEdtRdr', $oldRaiderClass, $editRaiderName, $editRaiderClass, $editRaiderRole, $oldRaiderName, $pun_user['username'], $oldRaiderRole, 'now', '');
}
// -------------------------------------------------------------
function showSelectDrops($raidDetailArray)
{
	$mobs = getMobsArray();
	$items = getItemsArray();
	$members = getRaidMembersArray($_GET['r']);
	echo '<form method="post" action="index.php?page=dkpraids&action=update&r='.$_GET['r'].'" name="addItemToRaid">';
	echo '<div id="accordion">';
	$firstElement = true;
	foreach ($mobs as $mob) {
		if ($mob[mobDungeonID] == $raidDetailArray[0][DungeonID]) {
			if (!$firstElement)
				echo '</div>';
			echo '<a href="#selectDrops" class="toggler"><b>'.$mob[mobName].'</b></a><br /><div class="element">';
			$firstElement = false;
			foreach ($items as $item) {
				if ($item[mobID] == $mob[mobID]) {
					if ($item[itemValue] == 0)
						echo '&nbsp;<input type="radio" name="addItemToRaid" value="'.$item[itemID].'"><span style="font-size: 8px;">free</span>&nbsp;';
					if ($item[itemValue] > 0) {
						echo '&nbsp;<input type="radio" name="addItemToRaid" value="'.$item[itemID].'"><span style="font-size: 8px;">need</span>&nbsp;';
						echo getItemDisplay($item[gameItemID], getItemColour($item['gameItemID']), $item[itemImage], $item[itemName], $item[itemValue]);
						echo '<br />';
					}
				}
			}
		}
	}
	echo '</div></div>';
	echo '<p style="text-align: right"><select name="addItemToRaider">';
	foreach ($members as $memberID => $memberName) {
		echo '<option value="'.$memberID.'">'.$memberName.'</option>';
	}
	echo '</select>';
	echo '&nbsp;&nbsp;<input type="submit" value="Add item"></p></form>';
}
// -------------------------------------------------------------
function addItemToRaid()
{
 	global $db, $pun_user;
	$db->query("
			INSERT INTO
				dmzdkp_balance (ItemID, MemberID, RaidID, Date)
			VALUES
				(".$_POST[addItemToRaid].", ".$_POST[addItemToRaider].", ".$_GET['r'].", '".date('Y-m-d H:i:s')."')
				;")
		or error('Unable to add item to raid', __FILE__, __LINE__, $db->error());
//	function addLogEntry($class, $code, $decode, $value, $reference, $title, $author, $authoriser, $start_date, $end_date)
//	addLogEntry('dkpLEditRd', 'Add', $unixDate, $dungeon, $tier, '', $pun_user['username'], '', 'now', '');
}
// -------------------------------------------------------------
function removeItemFromRaid()
{
 	global $db, $pun_user;
	$raidID = $_GET['r'];
	$removeItemFromRaid = $_POST["removeItemFromRaid"];
	foreach ($removeItemFromRaid as $item) {
		$toRemove = '';
		$toRemove .= "DELETE FROM dmzdkp_balance WHERE EntryID = ".$item." LIMIT 1;";
		$db->query($toRemove)
			or error('Unable to remove item(s)', __FILE__, __LINE__, $db->error());
	}

//	function addLogEntry($class, $code, $decode, $value, $reference, $title, $author, $authoriser, $start_date, $end_date)
//	addLogEntry('dkpLEditRd', 'Remove', $unixDate, $dungeon, $tier, '', $pun_user['username'], '', 'now', '');
}
// -------------------------------------------------------------
function showRaidLoot($raidID)
{
	$raidItems = getRaidItems($raidID);
	if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'update' && isset($_GET['r']))
		echo '<form method="post" action="index.php?page=dkpraids&action=update&r='.$_GET['r'].'" name="removeItemFromRaid">';
	foreach ($raidItems as $item)
	{
		$itemColour = getItemColour($item['gameItemID']);
		if ($currentMobName != $item[mobName])
		{
			if ($currentMemberName == 'Bob')
				echo '</table>';
			echo '<div style="font-weight: bold; border-bottom: 1px solid #CCC; margin: 2px; padding: 2px">'.$item[mobName].'</div>';
			echo '<table>';
			$currentMobName = $item[mobName];
			$currentMemberName = 'Bob';
		}
		echo '<tr><td width="75%">';
		if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'update' && isset($_GET['r']))
			echo '<input type="checkbox" name="removeItemFromRaid[]" value="'.$item[EntryID].'">&nbsp;';
		echo getItemDisplay($item[gameItemID], getItemColour($item['gameItemID']), $item[itemImage], $item[itemName], $item[Value]);
		echo '</td><td>';
		displayRaider($item[MemberID], $item[RoleImage], $item[RoleName], $item[ClassImage], $item[ClassName], $item[ClassColour], $item[MemberName], '<br />');
		echo '</td></tr>';
/*
		if ($currentMemberName != $item[MemberName])
		{
			displayRaider($item[MemberID], $item[RoleImage], $item[RoleName], $item[ClassImage], $item[ClassName], $item[ClassColour], $item[MemberName], '<br />');
			$currentMemberName = $item[MemberName];
		}
		if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'update' && isset($_GET['r']))
			echo '<input type="checkbox" name="removeItemFromRaid[]" value="'.$item[EntryID].'">&nbsp;';
		echo '&nbsp;'.getItemDisplay($item[gameItemID], getItemColour($item['gameItemID']), $item[itemImage], $item[itemName], $item[Value]).'<br />';
*/
	}
	echo '</table>';
	if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'update' && isset($_GET['r']))
		echo '<p style="text-align: right"><input type="submit" value="Remove item(s)"></p></form>';
//	print_r($raidItems);
}
// -------------------------------------------------------------
function displayChart($raidersArray)
{
$widthMultiplier = 20;
$width = 300;
if (($raidersArray[$raidersArray[largestRole]][total] * $widthMultiplier) > $width)
	$widthMultiplier = floor($width / $raidersArray[$raidersArray[largestRole]][total]);
?>
	<table>
		<tr>
			<td width="100px" style="text-align: right; font-weight: bold;">Tanks: </td>
			<td width="22px" style="text-align: center;"><?php echo $raidersArray[tanks][total]; ?></td>
			<td width="17px"><img src="./images/wow_icons/Ability_Warrior_ShieldMastery.png" height="15px" width="15px" title="Tanks: <?php echo $raidersArray[tanks][total]; ?>" /></td>
			<td>
				<img src="./images/common/sp_000000.gif" height="14px" width="1px" ><img src="./images/common/sp_deathknight.gif" height="14px" width="<?php echo ($raidersArray[tanks][deathknight] * $widthMultiplier); ?>" title="Death Knight: <?php echo $raidersArray[tanks][deathknight]; ?>"><img src="./images/common/sp_hunter.gif" height="14px" width="<?php echo ($raidersArray[tanks][hunter] * $widthMultiplier); ?>" title="Hunter: <?php echo $raidersArray[tanks][hunter]; ?>"><img src="./images/common/sp_druid.gif" height="14px" width="<?php echo ($raidersArray[tanks][druid] * $widthMultiplier); ?>" title="Druid: <?php echo $raidersArray[tanks][druid]; ?> "><img src="./images/common/sp_mage.gif" height="14px" width="<?php echo ($raidersArray[tanks][mage] * $widthMultiplier); ?>" title="Mage: <?php echo $raidersArray[tanks][mage]; ?>"><img src="./images/common/sp_paladin.gif" height="14px" width="<?php echo ($raidersArray[tanks][paladin] * $widthMultiplier); ?>" title="Paladin: <?php echo $raidersArray[tanks][paladin]; ?>"><img src="./images/common/sp_priest.gif" height="14px" width="<?php echo ($raidersArray[tanks][priest] * $widthMultiplier); ?>" title="Priest: <?php echo $raidersArray[tanks][priest]; ?>"><img src="./images/common/sp_rogue.gif" height="14px" width="<?php echo ($raidersArray[tanks][rogue] * $widthMultiplier); ?>" title="Rogue: <?php echo $raidersArray[tanks][rogue]; ?>"><img src="./images/common/sp_shaman.gif" height="14px" width="<?php echo ($raidersArray[tanks][shaman] * $widthMultiplier); ?>" title="Shaman: <?php echo $raidersArray[tanks][shaman]; ?>"><img src="./images/common/sp_warlock.gif" height="14px" width="<?php echo ($raidersArray[tanks][warlock] * $widthMultiplier); ?>" title="Warlock: <?php echo $raidersArray[tanks][warlock]; ?>"><img src="./images/common/sp_warrior.gif" height="14px" width="<?php echo ($raidersArray[tanks][warrior] * $widthMultiplier); ?>" title="Warrior: <?php echo $raidersArray[tanks][warrior]; ?>"><img src="./images/common/sp_000000.gif" height="14px" width="1px" >
			</td>
		</tr>
		<tr>
			<td width="100px" style="text-align: right; font-weight: bold;">Healers: </td>
			<td width="22px" style="text-align: center;"><?php echo $raidersArray[healers][total]; ?></td>
			<td width="17px"><img src="./images/wow_icons/Spell_Holy_Heal.png" height="15px" width="15px" title="Healers: <?php echo $raidersArray[healers][total]; ?>" /></td>
			<td>
				<img src="./images/common/sp_000000.gif" height="14px" width="1px" ><img src="./images/common/sp_deathknight.gif" height="14px" width="<?php echo ($raidersArray[healers][deathknight] * $widthMultiplier); ?>" title="Death Knight: <?php echo $raidersArray[healers][deathknight]; ?>"><img src="./images/common/sp_hunter.gif" height="14px" width="<?php echo ($raidersArray[healers][hunter] * $widthMultiplier); ?>" title="Hunter: <?php echo $raidersArray[healers][hunter]; ?>"><img src="./images/common/sp_druid.gif" height="14px" width="<?php echo ($raidersArray[healers][druid] * $widthMultiplier); ?>" title="Druid: <?php echo $raidersArray[healers][druid]; ?> "><img src="./images/common/sp_mage.gif" height="14px" width="<?php echo ($raidersArray[healers][mage] * $widthMultiplier); ?>" title="Mage: <?php echo $raidersArray[healers][mage]; ?>"><img src="./images/common/sp_paladin.gif" height="14px" width="<?php echo ($raidersArray[healers][paladin] * $widthMultiplier); ?>" title="Paladin: <?php echo $raidersArray[healers][paladin]; ?>"><img src="./images/common/sp_priest.gif" height="14px" width="<?php echo ($raidersArray[healers][priest] * $widthMultiplier); ?>" title="Priest: <?php echo $raidersArray[healers][priest]; ?>"><img src="./images/common/sp_rogue.gif" height="14px" width="<?php echo ($raidersArray[healers][rogue] * $widthMultiplier); ?>" title="Rogue: <?php echo $raidersArray[healers][rogue]; ?>"><img src="./images/common/sp_shaman.gif" height="14px" width="<?php echo ($raidersArray[healers][shaman] * $widthMultiplier); ?>" title="Shaman: <?php echo $raidersArray[healers][shaman]; ?>"><img src="./images/common/sp_warlock.gif" height="14px" width="<?php echo ($raidersArray[healers][warlock] * $widthMultiplier); ?>" title="Warlock: <?php echo $raidersArray[healers][warlock]; ?>"><img src="./images/common/sp_warrior.gif" height="14px" width="<?php echo ($raidersArray[healers][warrior] * $widthMultiplier); ?>" title="Warrior: <?php echo $raidersArray[healers][warrior]; ?>"><img src="./images/common/sp_000000.gif" height="14px" width="1px" >
			</td>
		</tr>
		<tr>
			<td width="100px" style="text-align: right; font-weight: bold;">Physical DPS: </td>
			<td width="22px" style="text-align: center;"><?php echo $raidersArray[pdps][total]; ?></td>
			<td width="17px"><img src="./images/wow_icons/Ability_Warrior_Challange.png" height="15px" width="15px" title="Physical DPS: <?php echo $raidersArray[pdps][total]; ?>" /></td>
			<td>
				<img src="./images/common/sp_000000.gif" height="14px" width="1px" ><img src="./images/common/sp_deathknight.gif" height="14px" width="<?php echo ($raidersArray[pdps][deathknight] * $widthMultiplier); ?>" title="Death Knight: <?php echo $raidersArray[pdps][deathknight]; ?>"><img src="./images/common/sp_hunter.gif" height="14px" width="<?php echo ($raidersArray[pdps][hunter] * $widthMultiplier); ?>" title="Hunter: <?php echo $raidersArray[pdps][hunter]; ?>"><img src="./images/common/sp_druid.gif" height="14px" width="<?php echo ($raidersArray[pdps][druid] * $widthMultiplier); ?>" title="Druid: <?php echo $raidersArray[pdps][druid]; ?> "><img src="./images/common/sp_mage.gif" height="14px" width="<?php echo ($raidersArray[pdps][mage] * $widthMultiplier); ?>" title="Mage: <?php echo $raidersArray[pdps][mage]; ?>"><img src="./images/common/sp_paladin.gif" height="14px" width="<?php echo ($raidersArray[pdps][paladin] * $widthMultiplier); ?>" title="Paladin: <?php echo $raidersArray[pdps][paladin]; ?>"><img src="./images/common/sp_priest.gif" height="14px" width="<?php echo ($raidersArray[pdps][priest] * $widthMultiplier); ?>" title="Priest: <?php echo $raidersArray[pdps][priest]; ?>"><img src="./images/common/sp_rogue.gif" height="14px" width="<?php echo ($raidersArray[pdps][rogue] * $widthMultiplier); ?>" title="Rogue: <?php echo $raidersArray[pdps][rogue]; ?>"><img src="./images/common/sp_shaman.gif" height="14px" width="<?php echo ($raidersArray[pdps][shaman] * $widthMultiplier); ?>" title="Shaman: <?php echo $raidersArray[pdps][shaman]; ?>"><img src="./images/common/sp_warlock.gif" height="14px" width="<?php echo ($raidersArray[pdps][warlock] * $widthMultiplier); ?>" title="Warlock: <?php echo $raidersArray[pdps][warlock]; ?>"><img src="./images/common/sp_warrior.gif" height="14px" width="<?php echo ($raidersArray[pdps][warrior] * $widthMultiplier); ?>" title="Warrior: <?php echo $raidersArray[pdps][warrior]; ?>"><img src="./images/common/sp_000000.gif" height="14px" width="1px" >
			</td>
		<tr>
			<td width="100px" style="text-align: right; font-weight: bold;">Caster DPS: </td>
			<td width="22px" style="text-align: center;"><?php echo $raidersArray[cdps][total]; ?></td>
			<td width="17px"><img src="./images/wow_icons/Spell_Fire_SoulBurn.png" height="15px" width="15px" title="Caster DPS: <?php echo $raidersArray[cdps][total]; ?>" /></td>
			<td>
				<img src="./images/common/sp_000000.gif" height="14px" width="1px" ><img src="./images/common/sp_deathknight.gif" height="14px" width="<?php echo ($raidersArray[cdps][deathknight] * $widthMultiplier); ?>" title="Death Knight: <?php echo $raidersArray[cdps][deathknight]; ?>"><img src="./images/common/sp_hunter.gif" height="14px" width="<?php echo ($raidersArray[cdps][hunter] * $widthMultiplier); ?>" title="Hunter: <?php echo $raidersArray[cdps][hunter]; ?>"><img src="./images/common/sp_druid.gif" height="14px" width="<?php echo ($raidersArray[cdps][druid] * $widthMultiplier); ?>" title="Druid: <?php echo $raidersArray[cdps][druid]; ?> "><img src="./images/common/sp_mage.gif" height="14px" width="<?php echo ($raidersArray[cdps][mage] * $widthMultiplier); ?>" title="Mage: <?php echo $raidersArray[cdps][mage]; ?>"><img src="./images/common/sp_paladin.gif" height="14px" width="<?php echo ($raidersArray[cdps][paladin] * $widthMultiplier); ?>" title="Paladin: <?php echo $raidersArray[cdps][paladin]; ?>"><img src="./images/common/sp_priest.gif" height="14px" width="<?php echo ($raidersArray[cdps][priest] * $widthMultiplier); ?>" title="Priest: <?php echo $raidersArray[cdps][priest]; ?>"><img src="./images/common/sp_rogue.gif" height="14px" width="<?php echo ($raidersArray[cdps][rogue] * $widthMultiplier); ?>" title="Rogue: <?php echo $raidersArray[cdps][rogue]; ?>"><img src="./images/common/sp_shaman.gif" height="14px" width="<?php echo ($raidersArray[cdps][shaman] * $widthMultiplier); ?>" title="Shaman: <?php echo $raidersArray[cdps][shaman]; ?>"><img src="./images/common/sp_warlock.gif" height="14px" width="<?php echo ($raidersArray[cdps][warlock] * $widthMultiplier); ?>" title="Warlock: <?php echo $raidersArray[cdps][warlock]; ?>"><img src="./images/common/sp_warrior.gif" height="14px" width="<?php echo ($raidersArray[cdps][warrior] * $widthMultiplier); ?>" title="Warrior: <?php echo $raidersArray[cdps][warrior]; ?>"><img src="./images/common/sp_000000.gif" height="14px" width="1px" >
			</td>
		</tr>
	</table>
<?php
}
// -------------------------------------------------------------
function editRaider()
{
	$editRaiderID = $_POST["raiderID"];
	$editRaiderName = $_POST["editName"];
	$editRaiderClass = $_POST["editClass"];
	$editRaiderRole = $_POST["editRole"];
	$oldRaiderName = $_POST["oldRaiderName"];
	$oldRaiderClass = $_POST["oldRaiderClass"];
	$oldRaiderRole = $_POST["oldRaiderRole"];
   	global $db, $pun_user;
	$db->query("
			UPDATE
				dmzdkp_members
			SET
				Name = '".$editRaiderName."',
				ClassID = ".$editRaiderClass.",
				RoleID = ".$editRaiderRole."
			WHERE
				MemberID = ".$editRaiderID)
		or error('Unable to update raider info', __FILE__, __LINE__, $db->error());
//	function addLogEntry($class, $code, $decode, $value, $reference, $title, $author, $authoriser, $start_date, $end_date)
	addLogEntry('dkpLEdtRdr', $oldRaiderClass, $editRaiderName, $editRaiderClass, $editRaiderRole, $oldRaiderName, $pun_user['username'], $oldRaiderRole, 'now', '');
}
// -------------------------------------------------------------
function addRaider()
{
	$addRaiderName = $_POST["addName"];
	$addRaiderClass = $_POST["addClass"];
	$addRaiderRole = $_POST["addRole"];
/* 	echo "INSERT INTO
				dmzdkp_members (Name, ClassID, RoleID, Date)
			VALUES
				('".$addRaiderName."', ".$addRaiderClass.", ".$addRaiderRole.", '".date('Y-m-d H:i:s')."')"; */
 	global $db, $pun_user;
	$db->query("
			INSERT INTO
				dmzdkp_members (Name, ClassID, RoleID, Date)
			VALUES
				('".$addRaiderName."', ".$addRaiderClass.", ".$addRaiderRole.", '".date('Y-m-d H:i:s')."')
		")
		or error('Unable to add raider', __FILE__, __LINE__, $db->error());
//	function addLogEntry($class, $code, $decode, $value, $reference, $title, $author, $authoriser, $start_date, $end_date)
	addLogEntry('dkpLAddRdr', '', $addRaiderName, $addRaiderClass, $addRaiderRole, '', $pun_user['username'], '', 'now', '');
//	header("location:" . $_SERVER['PHP_SELF']);
}
// -------------------------------------------------------------
function displayRaiderInput($inputType)
{
//	echo '<br />'.$inputType;
//	print_r($inputType);
	if ($inputType == 'add')
	{
		$inputTitle = 'Add new raider:';
		$inputPrefix = 'add';
		$inputButtonText = 'Add';
		$currentName = 'value=""';
	} else {
		$inputTitle = 'Edit raider:';
		$inputPrefix = 'edit';
		$inputButtonText = 'Update';
		$currentName = 'value="'.$inputType[0][Name].'"';
		$currentClass = $inputType[0][ClassID];
		$currentRole = $inputType[0][RoleID];
		$raiderID = '<input type="hidden" name="raiderID" value="'.$inputType[0][MemberID].'">';
		$postCurrentName = '<input type="hidden" name="oldRaiderName" value="'.$inputType[0][Name].'">';
		$postCurrentClass = '<input type="hidden" name="oldRaiderClass" value="'.$inputType[0][ClassID].'">';
		$postCurrentRole = '<input type="hidden" name="oldRaiderRole" value="'.$inputType[0][RoleID].'">';
	}
	foreach (getClassesArray('Name') as $class)
	{
		$selected = '';
		if ($currentClass == $class[ClassID])
			$selected = ' selected="selected"';
		$classList .= '<option value="'.$class[ClassID].'"'.$selected.'>'.$class[Name].'</option>';
	}
	foreach (getrolesArray('RoleID') as $role)
	{
		$selected = '';
		if ($currentRole == $role[RoleID])
			$selected = ' selected="selected"';
		$roleList .= '<option value="'.$role[RoleID].'"'.$selected.'>'.$role[RoleName].'</option>';
	}
?>
<div style="margin: 2px 4px 4px 4px; padding: 5px 0px 8px 0px; border-top: 1px solid #CCC; border-bottom: 1px solid #CCC;">
<p style="font-weight: bold"><?php echo $inputTitle; ?></p>
	<form method="post" action="index.php?page=dkpraiders&action=update" name="<?php echo $inputPrefix; ?>Raider">
		Name:&nbsp;<input type="text" size="16" maxlength="24" name="<?php echo $inputPrefix; ?>Name"<?php echo $currentName; ?>>&nbsp;
		Class:&nbsp;<select name="<?php echo $inputPrefix; ?>Class"><?php echo $classList; ?></select>&nbsp;
		Role:&nbsp;<select name="<?php echo $inputPrefix; ?>Role"><?php echo $roleList; ?></select>&nbsp;
		<?php echo $raiderID.$postCurrentName.$postCurrentClass.$postCurrentRole; ?>
		<input type="submit" value="<?php echo $inputButtonText; ?>">
	</form>
</div>
<?php
}
// -------------------------------------------------------------
Function removeRaiders()
{
	$removeRaiders = $_POST["removeRaiders"];
	foreach ($removeRaiders as $raider) {$toRemove .= $raider.", ";}
	$toRemove = substr($toRemove, 0, -2);
	global $db, $pun_user;
	$db->query("
			UPDATE
				dmzdkp_members
			SET
				Enabled = 0
			WHERE
				MemberID in (".$toRemove.")
		")
		or error('Unable to remove raiders', __FILE__, __LINE__, $db->error());
//	header("location:" . $_SERVER['PHP_SELF']);
//	function addLogEntry($class, $code, $decode, $value, $reference, $title, $author, $authoriser, $start_date, $end_date)
	addLogEntry('dkpLRemRdr', '', $toRemove, '', '', '', $pun_user['username'], '', 'now', '');
}
// -------------------------------------------------------------
Function activateRaiders()
{
	$activateRaiders = $_POST["activateRaiders"];
	foreach ($activateRaiders as $raider) {$toActivate .= $raider.", ";}
	$toActivate = substr($toActivate, 0, -2);
	global $db, $pun_user;
	$db->query("
			UPDATE
				dmzdkp_members
			SET
				Enabled = 1
			WHERE
				MemberID in (".$toActivate.")
		")
		or error('Unable to activate raiders', __FILE__, __LINE__, $db->error());
//	header("location:" . $_SERVER['PHP_SELF']);
//	function addLogEntry($class, $code, $decode, $value, $reference, $title, $author, $authoriser, $start_date, $end_date)
	addLogEntry('dkpLActRdr', '', $toActivate, '', '', '', $pun_user['username'], '', 'now', '');
}
// -------------------------------------------------------------
function getBunusExpiration()
{
	return (60*60*24*7*6); // 6 weeks
}
// -------------------------------------------------------------
function displayActiveRaiders($raidersArray, $addedRaiders, $signupArray)
{
	if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'update' && !isset($_GET['r']))
		echo '<form method="post" action="index.php?page=dkpraiders&action=update" name="removeRaiders">';
	if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'update' && isset($_GET['r']))
		echo '<form method="post" action="index.php?page=dkpraids&action=update&r='.$_GET['r'].'" name="addMemberToRaid">';
	?>
	<br />
	<table>
		<tr>
			<td valign="top" style="font-weight: bold">
				<?php
				$count = 0;
				$currRole = 0;
				foreach ($raidersArray as $item) {
					if ($item[isRaiding] == 1 && !in_array($item[MemberID], $addedRaiders)) {
						if ($item[roleID] == $currRole) {
							//Do nothing
						} else {
							$currRole = $item[roleID];
							$count++;
						}
						if ($count == 2) {
							echo '<br />';
							$count++;
						}
						if ($count == 4 || $count == 6) {
							echo '</td><td valign="top" style="font-weight: bold">';
							$count++;
						}
						$fontWeight = 'normal';
						if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'update') {
							foreach ($signupArray as $signee) {
								$pos = strpos(strtolower(html_entity_decode($signee)), strtolower($item[userName]));
								if ($pos === false) {
									// do nothing
								} else {
									$fontWeight = 'bold';
								}
							}
						}
						if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'update' && !isset($_GET['r']))
							echo '<a href="index.php?page=dkpraiders&action=update&edit='.$item[MemberID].'"><img src="./images/common/icons/cog.png" /></a>&nbsp;<input type="checkbox" name="removeRaiders[]" value="'.$item[MemberID].'">&nbsp;';
						if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'update' && isset($_GET['r']))
							echo '<input type="checkbox" name="addMemberToRaid[]" value="'.$item[MemberID].'">&nbsp;';
							
						$nameFormat = array("lineEnd" => "<br />", "titleText" => "", "fontWeight" => $fontWeight, "roleImage" => true, "classImage" => false);
						echo getRaiderDisplayName($item[MemberID], getRaiderDisplayArray(), $nameFormat);
						//displayRaider($item[MemberID], $item[roleImage], $item[roleName], $item[classImage], $item[className], $item[classColour], $item[userName], '<br />', '', $fontWeight);
					}
				}
				?>
			</td>
		</tr>
	</table>
	<?php
	if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'update' && !isset($_GET['r']))
		echo '<p style="text-align: right">With selected: <input type="submit" value="Deactivate"></p></form>';
	if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'update' && isset($_GET['r']))
		echo '<p style="text-align: right">With selected: <input type="submit" value="Add to raid"></p></form>';
}
// -------------------------------------------------------------
function removeMemberFromRaid()
{
 	global $db, $pun_user;

	$raidID = $_GET['r'];
	$removeMemberFromRaid = $_POST["removeMemberFromRaid"];
	foreach ($removeMemberFromRaid as $raider) {
		$toRemove = '';
		$toRemove .= "DELETE FROM dmzdkp_raidmembers WHERE RaidID = ".$raidID." AND MemberID = ".$raider." LIMIT 1;";
		$db->query($toRemove)
			or error('Unable to remove raiders', __FILE__, __LINE__, $db->error());
	}

//	function addLogEntry($class, $code, $decode, $value, $reference, $title, $author, $authoriser, $start_date, $end_date)
//	addLogEntry('dkpLEditRd', 'Remove', $unixDate, $dungeon, $tier, '', $pun_user['username'], '', 'now', '');
}
// -------------------------------------------------------------
function addMemberToRaid()
{
	$raidID = $_GET['r'];
	$addMemberToRaid = $_POST["addMemberToRaid"];
	foreach ($addMemberToRaid as $raider) {$toAdd .= "('".$raidID."', '".$raider."'), ";}
	$toAdd = substr($toAdd, 0, -2);
 	global $db, $pun_user;
	$db->query("
			INSERT INTO
				dmzdkp_raidmembers (RaidID, MemberID)
			VALUES
				".$toAdd."
				;")
		or error('Unable to add raiders', __FILE__, __LINE__, $db->error());
//	function addLogEntry($class, $code, $decode, $value, $reference, $title, $author, $authoriser, $start_date, $end_date)
//	addLogEntry('dkpLEditRd', 'Add', $unixDate, $dungeon, $tier, '', $pun_user['username'], '', 'now', '');
}
// -------------------------------------------------------------
function displayNonactiveRaiders($raidersArray)
{
	if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'update')
	{
		echo '<div style="margin: 2px 4px 4px 4px; padding: 5px 0px 8px 0px; border-top: 1px solid #CCC;"></div>';
		echo '<p style="font-weight: bold">Manage inactive raiders:</p>';
		echo '<form method="post" action="index.php?page=dkpraiders&action=update" name="activateRaiders">';
		?>
		<br />
		<table>
			<tr>
				<td valign="top" style="font-weight: bold">
					<?php
					$count = 0;
					$currRole = 0;
					foreach ($raidersArray as $item)
					{
						if ($item[isRaiding] == 0)
						{
							if ($item[roleID] == 't' || $item[roleID] == 'h' || $item[roleID] == 'p' || $item[roleID] == 'c' || $item[roleID] == '')
							{
								//do nothing
							} else {
								if ($item[roleID] == $currRole)
								{
									//Do nothing
								} else {
									$currRole = $item[roleID];
									$count++;
								}
								if ($count == 2)
								{
									echo '<br />';
									$count++;
								}
								if ($count == 4 || $count == 6)
								{
									echo '</td><td valign="top" style="font-weight: bold">';
									$count++;
								}
								if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'update')
									echo '<a href="index.php?page=dkpraiders&action=update&edit='.$item[MemberID].'"><img src="./images/common/icons/cog.png" /></a>&nbsp;<input type="checkbox" name="activateRaiders[]" value="'.$item[MemberID].'">&nbsp;';

								displayRaider($item[MemberID], $item[roleImage], $item[roleName], $item[classImage], $item[className], $item[classColour], $item[userName], '<br />');
							}
						}
					}
					?>
				</td>
			</tr>
		</table>
		<?php
		echo '<p style="text-align: right">With selected: <input type="submit" value="Activate"></p>';
		echo '</form>';
	}
}
// -------------------------------------------------------------
function showNewRaidLink()
{
	echo '<img src="./images/common/icons/add.png"  title="Add new raid" style="vertical-align:middle;"/>&nbsp;
			<a href="http://dmz-gaming.com/index.php?page=dkpraids&action=new" title="Add new raid" style="vertical-align:middle;">
				Add new raid
			</a>';
}
// -------------------------------------------------------------
function showNewItemLink()
{
	echo '<img src="./images/common/icons/add.png"  title="Add new item"/>
			<a href="http://dmz-gaming.com/index.php?page=dkpitems&action=new" title="Add new item">
				Add new item
			</a>';
}
// -------------------------------------------------------------
function addRaid()
{
	$unixDate = mktime(12, 0, 0, $_POST['month'], $_POST['day'], $_POST['year']);
	$date = date('Y-m-d', $unixDate).' 12:00:00'; // Format: YYYY-MM-DD 00:00:00
	$dungeonAndTier = explode(",", $_POST["dungeon"]);
	$dungeon = $dungeonAndTier[0];
	$tier = $dungeonAndTier[1];

 	global $db, $pun_user;
	$db->query("
			INSERT INTO
				dmzdkp_raids (DungeonID, Date, hidden)
			VALUES
				('".$dungeon."', '".$date."', 1)
		")
		or error('Unable to add raid', __FILE__, __LINE__, $db->error());
//	function addLogEntry($class, $code, $decode, $value, $reference, $title, $author, $authoriser, $start_date, $end_date)
	addLogEntry('dkpLAdRaid', '', $unixDate, $dungeon, $tier, '', $pun_user['username'], '', 'now', '');
}
// -------------------------------------------------------------
function displayCreateRaidForm()
{
	echo '<form method="post" action="index.php?page=dkpraids" name="addRaid">';
	$dungeonsArray = array_reverse(getDungeonsArray());
	?>
	<p><b>Select date</b></p>
	<select name="day" tabindex="2">
	<?php
		$min = 1; $max = 32; $i = $min;
		$today = date('j');
		while ($i < $max) {
			$selected = '';
			if ($i == $today)
				$selected = ' SELECTED';
			echo '<option value="'.$i.'"'.$selected.'>'.$i.'</option>
			';
			$i++;
		}
	?>
	</select> /
	<select name="month" tabindex="3">
	<?php
		$min = 1; $max = 13; $i = $min;
		$today = date('n');
		while ($i < $max) {
			$selected = '';
			if ($i == $today)
				$selected = ' SELECTED';
			echo '<option value="'.$i.'"'.$selected.'>'.date('F', mktime(0, 0, 0, $i+1, 0, 0)).'</option>
			';
			$i++;
		}
	?>
	</select> /
	<select name="year" tabindex="4">
	<?php
		$min = date('Y')-2; $max = $min + 5; $i = $min;
		$today = date('Y');
		while ($i < $max) {
			$selected = '';
			if ($i == $today)
				$selected = ' SELECTED';
			echo '<option value="'.$i.'"'.$selected.'>'.$i.'</option>
			';
			$i++;
		}
	?>
	</select>
	<br /><br />
	<p><b>Select instance</b></p>
	<?php
//	print_r($dungeonsArray);
	foreach ($dungeonsArray as $d) {
		if ($d[dungeonGameID] != 0) {
			echo '<input type="radio" name="dungeon" value="'.$d[dungeonID].','.$d[dungeonTier].'">&nbsp;<img src="./site/dkp/images/instances/tier'.$d[dungeonTier].'.gif" title="Tier '.$d[dungeonTier].'"/>&nbsp;'.$d[dungeonName].'<br />';
		}
	}
	echo '<p style="text-align: right"><input type="submit" name="addRaid" value="Create raid"></p>';
	echo '</form>';
}
// -------------------------------------------------------------
function showRaiderDetails($raiderID)
{
	$dungeonDetail = getDungeonsArray();
	$raiderDetail = getRaiderDetail($raiderID);
	echo '<table cellspacing="0" style="border-bottom: 1px solid #CCC;">';
	$previousRaid = '0';
	foreach ($raiderDetail as $raid) {
		if ($raid[hidden] == 0 || isEditable()) {
			$isCurrentRow = false;
			$defaultRowColour = 'f1f1f1';
			if ($raid[hidden] == 1)
				$defaultRowColour = 'e49d9d';
			if ($previousRaid != $raid[RaidID])
				showRaidRow($raid[RaidID], $raid[Date], $dungeonDetail[$raid[DungeonID]][dungeonName], $dungeonDetail[$raid[DungeonID]][dungeonTier], $defaultRowColour, $isCurrentRow, $raid[wws_url], $raid[BonusValue], round($raid[RaidValue],0));
			if ($raid[ItemID] != NULL && $raid[ItemMemberID] == $raid[MemberID]) {
				?>
					<tr bgcolor="#<?php echo $defaultRowColour; ?>" onMouseOver="this.bgColor='#ccb';" onMouseOut="this.bgColor='#<?php echo $defaultRowColour; ?>';">
						<td width="17px" style="padding: 3px 4px 1px 4px; margin: 0px 0px 0px 0px;">

						</td>
						<td colspan="7" style="padding: 1px 4px 1px 4px; margin: 0px 0px 0px 0px;">
							<?php
								echo getItemDisplay($raid[gameItemID], getItemColour($raid['gameItemID']), $raid[image], $raid[Name], $raid[ItemValue]);
							?>
						</td>
					</tr>
				<?php
			}
			$previousRaid = $raid[RaidID];
		}
	}
	echo '</table>';
}
// -------------------------------------------------------------
function showDKPTitlebar($titleString)
{
?>
    		<table cellspacing="0">
    			<tr>
    				<td><h2 style="border-right-width: 0px;"><span><?php echo $titleString; ?></span></h2></td>
    				<td width="75px" style="text-align: right; padding: 0px">
    					<h2 style="border-left-width: 0px; padding: 3px 4px 0px 0px">
    						<a href="index.php?page=dkptables" title="View DKP standings"><img src="./images/wow_icons/Achievement_BG_winbyten.png" height="16px" width="16px"/></a>
    						<a href="index.php?page=dkpraids" title="View DMZ raids"><img src="./images/wow_icons/INV_Helmet_06.png" height="16px" width="16px"/></a>
    						<a href="index.php?page=dkpraiders" title="View DMZ raiders"><img src="./images/wow_icons/INV_Misc_GroupLooking.png" height="16px" width="16px"/></a>
    					</h2>
    				</td>
    			</tr>
    		</table>
<?php
}
// -------------------------------------------------------------
function showStandingTable()
{
 	global $pun_user;
	?>
	<table class="table" cellspacing="0">
		<tr class="tablehead">
			<th width="20px" style="text-align: center; padding: 0px">
				<a href="./index.php?page=dkptables&o=r" title="Order by role">R</a></th>
			<th width="20px" style="text-align: center; padding: 0px">
				<a href="./index.php?page=dkptables&o=c" title="Order by class">C</a></th>
			<th width="105px" style="text-align: left; padding: 0px">
				<a href="./index.php?page=dkptables&o=n" title="Order by name">Name</a></th>
			<th width="35px" style="text-align: center; padding: 0px">
				<a href="./index.php?page=dkptables&o=d" title="Order by DKP value"><img src="./images/wow_icons/Achievement_BG_win_AV_X_times.png" height="16px" width="16px"/></a></th>
			<th width="20px" style="text-align: center; padding: 0px">
				<a href="./index.php?page=dkptables&o=b" title="Order by bonus value"><img src="./images/wow_icons/Spell_ChargePositive.png" height="16px" width="16px"/></a></th>
			<th width="35px" style="text-align: center; padding: 0px">
				<a href="./index.php?page=dkptables" title="Order by overall standing"><img src="./images/wow_icons/Achievement_BG_winbyten.png" height="16px" width="16px"/></a></th>
			<th></th>
		</tr>
	<?php
	$order = 'Standing DESC, DKP DESC';
	if (isset($_GET['o'])) {
		switch ($_GET['o']) {
			case 'r': $order = 'RoleID, Standing DESC'; break;
			case 'c': $order = 'ClassID, Standing DESC'; break;
			case 'n': $order = 'MemberName, Standing DESC'; break;
			case 'd': $order = 'DKP DESC, Standing DESC'; break;
			case 'b': $order = 'Bonus DESC, Standing DESC'; break;
			default : $order = 'Standing DESC, DKP DESC'; break;
		}
	}
	$standings = getStandingsArray($order);
	$tonightsRaiders = getTonightsRaidersArray();
	$highestStanding = 0;
	$lowestStanding = 0;
	foreach ($standings as $member){
		if ($member[Standing] > $highestStanding)
			$highestStanding = $member[Standing];
		if ($member[DKP] < $lowestStanding)
			$lowestStanding = $member[DKP];
	}
	$differential = ($highestStanding - $lowestStanding);
	if ($differential == 0)
		$differential = 1;
	$pixelWidth = (306 / $differential); // error when zero
	$zeroPoint = floor($pixelWidth * (-1*$lowestStanding));
	foreach ($standings as $member){
		$rowColour = '#bcb';
		if (!isset($tonightsRaiders[$member[MemberID]]))
			$rowColour = '#f1f1f1';
		if (round($member[Standing]) < 0) {
			$negativeWidth = round(((-1 * ($member[DKP])) - $member[Bonus]) * $pixelWidth);
			$negativeBonusWidth = round(($member[Bonus] * $pixelWidth));
			$blankWidth = $zeroPoint - $negativeBonusWidth - $negativeWidth;
			if ($blankWidth < 0)
				$blankWidth = 0;
			$positiveWidth = 0;
			$positiveBonusWidth = 0;
		} else {
			$blankWidth = $zeroPoint;
			$negativeBonusWidth = 0;
			$negativeWidth = 0;
			$positiveWidth = round(($member[DKP]) * $pixelWidth);
			$positiveBonusWidth = round($member[Bonus] * $pixelWidth);
			if ((round($member[DKP])) < 0) {
				$positiveBonusWidth = round(($member[Bonus] - (-1 * ($member[DKP]))) * $pixelWidth);
				$negativeBonusWidth = round(-1 * (($member[DKP])) * $pixelWidth);
				$blankWidth = $zeroPoint - $negativeBonusWidth;
			}
		}
		if ($member[Enabled] == 1) { // || $pun_user['id'] == 2 || $pun_user['id'] == 551
				?>	<tr bgcolor="<?php echo $rowColour; ?>" onMouseOver="this.bgColor='#ccb';" onMouseOut="this.bgColor='<?php echo $rowColour; ?>';"> <?php
			echo '	<td  style="text-align: center; padding: 1px 0px 0px 0px"><img src="'.$member[RoleImage].'" height="15px" width="15px" title="'.$member[RoleName].'" style="border: 0px; padding: 0px" /></td>
					<td  style="text-align: center; padding: 0px"><img src="./img/clandmz/'.$member[ClassImage].'" height="15px" width="15px" title="'.$member[ClassName].'" style="border: 0px; padding: 0px" /></td>
					<td  style="text-align: left; padding: 0px"><a href="index.php?page=dkptables&m='.$member[MemberID].'" style="color: #'.$member[ClassColour].'; font-weight: bold; text-decoration: none">'.$member[MemberName].'</a></td>
					<td style="text-align: center; padding: 0px">'.round($member[DKP]).'</td>
					<td style="text-align: center; padding: 0px">'.$member[Bonus].'</td>
					<td style="text-align: center; padding: 0px">'.round($member[Standing]).'</td>
					<td style="text-align: left; padding: 0px"><img src="./images/common/sp_blank.gif" height="14px" width="'.$blankWidth.'px"><img src="./images/common/sp_paladin.gif" height="14px" width="'.$negativeBonusWidth.'px"><img src="./images/common/sp_deathknight.gif" height="14px" width="'.$negativeWidth.'px"><img src="./images/common/sp_000000.gif" height="14px" width="1px"><img src="./images/common/sp_shaman.gif" height="14px" width="'.$positiveWidth.'px"><img src="./images/common/sp_mage.gif" height="14px" width="'.$positiveBonusWidth.'px"></td>
				</tr>';
		}
	}
	echo '</table>';
}
// -------------------------------------------------------------
function getStandingsArray($order)
{
	global $db;
	$standingsTable = 'dmzdkp_memberStandingStatic';
	if (isset($_GET['live']))
		$standingsTable = 'dmzdkp_memberStanding';
	$standingsArray = array();
	$result = $db->query('
			SELECT
				m.MemberID,
				s.DKP,
				s.Bonus,
				s.Standing,
				m.Name AS MemberName,
				m.ClassID,
				m.RoleID,
				m.Enabled,
				c.Name AS ClassName,
				c.Colour AS ClassColour,
				c.Image AS ClassImage,
				r.RoleName,
				r.Image AS RoleImage
			FROM
				dmzdkp_members m,
				'.$standingsTable.' s,
				dmzdkp_classes c,
				dmzdkp_roles r
			WHERE
				m.MemberID = s.MemberID
			AND	c.ClassID = m.ClassID
			AND	r.RoleID = m.RoleID
			ORDER BY '.$order.'
		', true) or error('Unable to fetch standings data', __FILE__, __LINE__, $db->error());

	while ($cur_row = $db->fetch_assoc($result))
	{
		$standingsArray[] = array(
			'MemberID' => $cur_row['MemberID'],
			'DKP' => $cur_row['DKP'],
			'Bonus' => $cur_row['Bonus'],
			'Standing' => $cur_row['Standing'],
			'MemberName' => $cur_row['MemberName'],
			'ClassID' => $cur_row['ClassID'],
			'ClassName' => $cur_row['ClassName'],
			'ClassColour' => $cur_row['ClassColour'],
			'ClassImage' => $cur_row['ClassImage'],
			'RoleID' => $cur_row['RoleID'],
			'RoleName' => $cur_row['RoleName'],
			'RoleImage' => $cur_row['RoleImage'],
			'Enabled' => $cur_row['Enabled'],
			);
	}
	return $standingsArray;
}// -------------------------------------------------------------
function getTonightsRaidersArray()
{
	global $db;
	$tonightsRaidersArray = array();
	$result = $db->query('
			SELECT
				*
			FROM
				dmzdkp_raidmembers
			WHERE
				RaidID IN (
					SELECT
						RaidID
					FROM
						dmzdkp_raids
					WHERE
						DATE(Date) = CURDATE()
					AND hidden = 0
				)
		', true) or error('Unable to fetch tonight\'s raider data', __FILE__, __LINE__, $db->error());

	while ($cur_row = $db->fetch_assoc($result))
	{
		$tonightsRaidersArray[$cur_row['MemberID']] = 1;		
	}
	return $tonightsRaidersArray;
}
// -------------------------------------------------------------
function getRaidItems($raidID)
{
	global $db;
	$itemArray = array();
	$result = $db->query('
			SELECT
				b.EntryID,
				b.ItemID,
				b.MemberID,
				i.MobID,
				i.Name as itemName,
				v.ItemValue as Value,
				i.gameItemID,
				i.image as itemImage,
				o.Name as mobName,
				o.mobImage,
				m.Name as MemberName,
				m.ClassID,
				m.RoleID,
				c.Name as ClassName,
				c.colour as ClassColour,
				c.image as ClassImage,
				l.RoleName,
				l.image as RoleImage
			FROM
				dmzdkp_balance b,
				dmzdkp_items i,
				dmzdkp_itemValues v,
				dmzdkp_mobs o,
				dmzdkp_members m,
				dmzdkp_classes c,
				dmzdkp_roles l
			WHERE
				b.RaidID = '.$raidID.'
			AND i.ItemID = b.ItemID
			AND v.ItemID = b.ItemID
			AND	o.MobID = i.MobID
			AND m.MemberID = b.MemberID
			AND	c.ClassID = m.ClassID
			AND l.RoleID = m.RoleID
			ORDER BY i.MobID, b.EntryID, MemberName
		', true) or error('Unable to fetch raid data', __FILE__, __LINE__, $db->error());

	while ($cur_row = $db->fetch_assoc($result))
	{
		$itemArray[] = array(
			'EntryID' => $cur_row['EntryID'],
			'ItemID' => $cur_row['ItemID'],
			'MemberID' => $cur_row['MemberID'],
			'MobID' => $cur_row['MobID'],
			'itemName' => $cur_row['itemName'],
			'Value' => $cur_row['Value'],
			'gameItemID' => $cur_row['gameItemID'],
			'itemImage' => $cur_row['itemImage'],
			'mobName' => $cur_row['mobName'],
			'mobImage' => $cur_row['mobImage'],
			'MemberName' => $cur_row['MemberName'],
			'ClassID' => $cur_row['ClassID'],
			'RoleID' => $cur_row['RoleID'],
			'ClassName' => $cur_row['ClassName'],
			'ClassColour' => $cur_row['ClassColour'],
			'ClassImage' => $cur_row['ClassImage'],
			'RoleName' => $cur_row['RoleName'],
			'RoleImage' => $cur_row['RoleImage']
			);
	}
	return $itemArray;
}
// -------------------------------------------------------------
function getGamesArray()
{
	global $db;
	$gamesArray = array();
	$result = $db->query('
			SELECT
				*
			FROM
				dmzdkp_games
			ORDER BY ID
		', true) or error('Unable to fetch game data', __FILE__, __LINE__, $db->error());

	while ($cur_row = $db->fetch_assoc($result))
	{
		$gamesArray[] = array(
			'gameID' => $cur_row['ID'],
			'gameName' => $cur_row['name']
			);
	}
	return $gamesArray;
}
// -------------------------------------------------------------
function getDungeonsArray()
{
	global $db;
	$dungeonsArray = array();
	$result = $db->query('
			SELECT
				*
			FROM
				dmzdkp_dungeons
			ORDER BY gameID, Tier, DungeonID
		', true) or error('Unable to fetch dungeon data', __FILE__, __LINE__, $db->error());

	while ($cur_row = $db->fetch_assoc($result))
	{
		$dungeonsArray[$cur_row['DungeonID']] = array(
			'dungeonID' => $cur_row['DungeonID'],
			'dungeonName' => $cur_row['Name'],
			'dungeonTier' => $cur_row['Tier'],
			'dungeonGameID' => $cur_row['gameID']
			);
	}
	return $dungeonsArray;
}
// -------------------------------------------------------------
function getMobsArray()
{
	global $db;
	$mobsArray = array();
	$result = $db->query('
			SELECT
				*
			FROM
				dmzdkp_mobs
			ORDER BY mobID
		', true) or error('Unable to fetch mob data', __FILE__, __LINE__, $db->error());

	while ($cur_row = $db->fetch_assoc($result))
	{
		$mobsArray[$cur_row['MobID']] = array(
			'mobID' => $cur_row['MobID'],
			'mobName' => $cur_row['Name'],
			'mobDungeonID' => $cur_row['DungeonID'],
			'mobImage' => $cur_row['mobImage']
			);
	}
	return $mobsArray;
}
// -------------------------------------------------------------
function getRaidMembersArray($raidID)
{
	global $db;
	$raidMembersArray = array();
	$result = $db->query('
			SELECT
				r.MemberID,
				m.Name
			FROM
				dmzdkp_raidmembers r,
				dmzdkp_members m
			WHERE
				r.RaidID = '.$raidID.'
			AND	m.MemberID = r.MemberID
			ORDER BY m.Name
		', true) or error('Unable to fetch member list', __FILE__, __LINE__, $db->error());

	while ($cur_row = $db->fetch_assoc($result))
	{
		$raidMembersArray[$cur_row['MemberID']] = $cur_row['Name'];
	}
	return $raidMembersArray;
}
// -------------------------------------------------------------
function getItemsArray()
{
	global $db;
	$itemsArray = array();
	$result = $db->query('
			SELECT
				i.ItemID,
				i.MobID,
				i.Name,
				v.ItemValue,
				i.iLevel,
				i.SlotID,
				i.gameItemID,
				i.image
			FROM
				dmzdkp_items i,
				dmzdkp_itemValues v
			WHERE
				v.ItemID = i.ItemID
			ORDER BY mobID, Name, iLevel
		', true) or error('Unable to fetch item data', __FILE__, __LINE__, $db->error());

	while ($cur_row = $db->fetch_assoc($result))
	{
		$itemsArray[$cur_row['ItemID']] = array(
			'itemID' => $cur_row['ItemID'],
			'mobID' => $cur_row['MobID'],
			'itemName' => $cur_row['Name'],
			'itemValue' => $cur_row['ItemValue'],
			'iLevel' => $cur_row['iLevel'],
			'SlotID' => $cur_row['SlotID'],
			'gameItemID' => $cur_row['gameItemID'],
			'itemImage' => $cur_row['image'],
			);
	}
	return $itemsArray;
}
// -------------------------------------------------------------
function getRaiderInfo($memberID)
{
 	global $db, $pun_user;
//	echo '<br />'.$memberID.'<br />';
	$raiderInfoArray = array();
	$result = $db->query("
			SELECT
				MemberID, Name, ClassID, RoleID
			FROM
				dmzdkp_members
			WHERE
				MemberID = ".$memberID)
		or error('Unable to find raider info', __FILE__, __LINE__, $db->error());
	while ($cur_row = $db->fetch_assoc($result))
	{
//		echo '<br/>'.$cur_row['Name'];
//		echo '<br/>'.$cur_row['ClassID'];
//		echo '<br/>'.$cur_row['RoleID'];
		$raiderInfoArray[] = array(
			'MemberID' => $cur_row['MemberID'],
			'Name' => $cur_row['Name'],
			'ClassID' => $cur_row['ClassID'],
			'RoleID' => $cur_row['RoleID']
			);
	}
//	print_r($raiderInfoArray);
	return $raiderInfoArray;
}
// -------------------------------------------------------------
function getAllRaids()
{
	global $db;
	$raidArray = array();
	$result = $db->query('
			SELECT
				r.RaidID,
				r.DungeonID,
				r.BonusValue,
				r.Date,
				r.wws_url,
				r.hidden,
				d.Name as DungeonName,
				d.Tier as DungeonTier,
				v.RaidValue
			FROM
				dmzdkp_raids r,
				dmzdkp_dungeons d,
				dmzdkp_raidValues v
			WHERE
				d.DungeonID = r.DungeonID
			AND v.RaidID = r.RaidID
			AND d.Tier <> 0
			ORDER BY r.Date DESC, r.RaidID DESC
		', true) or error('Unable to fetch raid data', __FILE__, __LINE__, $db->error());

	while ($cur_row = $db->fetch_assoc($result))
	{
		$raidArray[$cur_row['RaidID']] = array(
			'RaidID' => $cur_row['RaidID'],
			'DungeonID' => $cur_row['DungeonID'],
			'BonusValue' => $cur_row['BonusValue'],
			'Date' => $cur_row['Date'],
			'wws_url' => $cur_row['wws_url'],
			'hidden' => $cur_row['hidden'],
			'DungeonName' => $cur_row['DungeonName'],
			'DungeonTier' => $cur_row['DungeonTier'],
			'RaidValue' => $cur_row['RaidValue']
			);
	}
	return $raidArray;
}
// -------------------------------------------------------------
function getRaidDetails($raidID)
{
	global $db;
	$raidArray = array();
	$result = $db->query('
			SELECT
				rm.MemberID,
				r.RaidID,
				r.DungeonID,
				r.BonusValue,
				r.Date,
				r.wws_url,
				r.hidden,
				d.Name as DungeonName,
				d.Tier as DungeonTier,
				m.Name as MemberName,
				m.ClassID,
				m.RoleID,
				c.Name as ClassName,
				c.colour as ClassColour,
				c.image as ClassImage,
				l.RoleName,
				l.image as RoleImage
			FROM
				dmzdkp_raidmembers rm,
				dmzdkp_raids r,
				dmzdkp_dungeons d,
				dmzdkp_members m,
				dmzdkp_classes c,
				dmzdkp_roles l
			WHERE
				rm.RaidID = '.$raidID.'
			AND r.RaidID = rm.RaidID
			AND	d.DungeonID = r.DungeonID
			AND m.MemberID = rm.MemberID
			AND	c.ClassID = m.ClassID
			AND l.RoleID = m.RoleID
			ORDER BY l.RoleID, ClassID, MemberName
		', true) or error('Unable to fetch raid data', __FILE__, __LINE__, $db->error());

	while ($cur_row = $db->fetch_assoc($result))
	{
		$raidArray[] = array(
			'MemberID' => $cur_row['MemberID'],
			'RaidID' => $cur_row['RaidID'],
			'DungeonID' => $cur_row['DungeonID'],
			'BonusValue' => $cur_row['BonusValue'],
			'Date' => $cur_row['Date'],
			'wws_url' => $cur_row['wws_url'],
			'hidden' => $cur_row['hidden'],
			'DungeonName' => $cur_row['DungeonName'],
			'DungeonTier' => $cur_row['DungeonTier'],
			'MemberName' => $cur_row['MemberName'],
			'ClassID' => $cur_row['ClassID'],
			'RoleID' => $cur_row['RoleID'],
			'ClassName' => $cur_row['ClassName'],
			'ClassColour' => $cur_row['ClassColour'],
			'ClassImage' => $cur_row['ClassImage'],
			'RoleName' => $cur_row['RoleName'],
			'RoleImage' => $cur_row['RoleImage']
			);
	}
	return $raidArray;
}
// -------------------------------------------------------------
function getAllItemArray($orderBy)
{
	global $db;
//	$orderBy = 'ItemName';
	$itemArray = array();
	$result = $db->query('
	SELECT
		i.ItemID,
		i.image,
		i.gameItemID,
		i.Name as ItemName,
		v.ItemValue AS Value,
		m.Name as MobName,
		d.DungeonID,
		d.Name as DungeonName
	FROM
		dmzdkp_items i,
		dmzdkp_itemValues v,
		dmzdkp_mobs m,
		dmzdkp_dungeons d
	WHERE
		m.MobID = i.MobID
	AND	v.ItemID = i.ItemID
	AND d.DungeonID = m.DungeonID
	ORDER BY '.$orderBy.', Value', true) or error('Unable to fetch item data', __FILE__, __LINE__, $db->error());

	while ($cur_row = $db->fetch_assoc($result))
	{
		$itemArray[] = array(
			'id' => $cur_row['ItemID'],
			'image' => $cur_row['image'],
			'code' => $cur_row['gameItemID'],
			'name' => $cur_row['ItemName'],
			'value' => $cur_row['Value'],
			'mob' => $cur_row['MobName'],
			'd_id' => $cur_row['DungeonID'],
			'instance' => $cur_row['DungeonName']
			);
	}
	return $itemArray;
}
// -------------------------------------------------------------
function getAllItemOrder()
{
	if (isset($_GET['dir'])) {
		$order = 'ItemName DESC';
		$nameDir = '';
	} else{
		$order = 'ItemName';
		$nameDir = '&dir=rev';
	}
	$raidDir = ''; $mobDir = ''; $valueDir = '';
	if (isset($_GET['order']))
	{
		switch ($_GET['order'])
		{
			case 'raid':
				if (isset($_GET['dir'])) {
					$order = 'd.DungeonID DESC';
					$raidDir = '';
				} else{
					$order = 'd.DungeonID';
					$raidDir = '&dir=rev';
				}
				$nameDir = ''; $mobDir = ''; $valueDir = '';
				break;
			case 'mob':
				if (isset($_GET['dir'])) {
					$order = 'MobName DESC';
					$mobDir = '';
				} else{
					$order = 'MobName';
					$mobDir = '&dir=rev';
				}
				$nameDir = ''; $raidDir = ''; $valueDir = '';
				break;
			case 'value':
				if (isset($_GET['dir'])) {
					$order = 'i.Value DESC';
					$valueDir = '';
				} else{
					$order = 'i.Value';
					$valueDir = '&dir=rev';
				}
				$nameDir = ''; $raidDir = ''; $mobDir = '';
				break;
		}
	}
	$orderArray = array(
					'order' => $order,
					'nameDir' => $nameDir,
					'raidDir' => $raidDir,
					'mobDir' => $mobDir,
					'valueDir' => $valueDir,
					);
	return $orderArray;
}
// -------------------------------------------------------------
function getRaidersArray()
{
	global $db;
	$pollArray = array();
	$result = $db->query('
	select
		m.Enabled as isRaiding,
		m.ClassID as classID,
		m.RoleID as roleID,
		m.MemberID,
		c.name as className,
		c.colour as classColour,
		c.image as classImage,
		r.RoleName as roleName,
		r.image as roleImage,
		m.Name
	from
		dmzdkp_members m,
		dmzdkp_classes c,
		dmzdkp_roles r
	where
		c.ClassID = m.ClassID
	and	r.RoleID = m.RoleID
	order by isRaiding DESC, m.RoleID, m.ClassID, m.Name
		', true) or error('Unable to fetch raider data (getRaidersArray)', __FILE__, __LINE__, $db->error());

	while ($cur_row = $db->fetch_assoc($result))
	{
		if ($cur_row['isRaiding'] == 1)
		{
			$classTag = str_replace(" ", "", strtolower($cur_row['className']));
			switch ($cur_row['roleID']) {
				case "1":
					$pollArray[tanks][total] = $pollArray[tanks][total] + 1;
					$pollArray[tanks][$classTag] = $pollArray[tanks][$classTag] + 1;
					if ($pollArray[tanks] > $pollArray[$pollArray[largestRole]]) {$pollArray[largestRole] = 'tanks';}
					break;
				case "2":
					$pollArray[healers][total] = $pollArray[healers][total] + 1;
					$pollArray[healers][$classTag] = $pollArray[healers][$classTag] + 1;
					if ($pollArray[healers] > $pollArray[$pollArray[largestRole]]) {$pollArray[largestRole] = 'healers';}
					break;
				case "3":
					$pollArray[pdps][total] = $pollArray[pdps][total] + 1;
					$pollArray[pdps][$classTag] = $pollArray[pdps][$classTag] + 1;
					if ($pollArray[pdps] > $pollArray[$pollArray[largestRole]]) {$pollArray[largestRole] = 'pdps';}
					break;
				case "4":
					$pollArray[cdps][total] = $pollArray[cdps][total] + 1;
					$pollArray[cdps][$classTag] = $pollArray[cdps][$classTag] + 1;
					if ($pollArray[cdps] > $pollArray[$pollArray[largestRole]]) {$pollArray[largestRole] = 'cdps';}
					break;
			}
		}
//function getRaidersArray()
		$pollArray[] = array(
			'isRaiding' => $cur_row['isRaiding'],
			'classID' => $cur_row['classID'],
			'className' => $cur_row['className'],
			'classColour' => $cur_row['classColour'],
			'classImage' => $cur_row['classImage'],
			'roleID' => $cur_row['roleID'],
			'roleName' => $cur_row['roleName'],
			'roleImage' => $cur_row['roleImage'],
			'MemberID' => $cur_row['MemberID'],
			'userName' => $cur_row['Name'],
		);
	}
	return $pollArray;
}
// -------------------------------------------------------------
function getRolesArray($order="RoleID") {
	global $db;
	$rolesArray = array();
	$result = $db->query('
	select
		RoleID, RoleName, image
	from
		dmzdkp_roles
	order by '.$order.'
		', true) or error('Unable to fetch role data', __FILE__, __LINE__, $db->error());

	while ($cur_row = $db->fetch_assoc($result))
	{
//function getRolesArray($order)
		$rolesArray[] = array(
			'RoleID' => $cur_row['RoleID'],
			'RoleName' => $cur_row['RoleName'],
			'image' => $cur_row['image']
			);
	}
	return $rolesArray;
}
// -------------------------------------------------------------
function getClassesArray($order="ClassID") {
	global $db;
	$classesArray = array();
	$result = $db->query('
	select
		ClassID, Name, colour, image
	from
		dmzdkp_classes
	order by '.$order.'
		', true) or error('Unable to fetch class data', __FILE__, __LINE__, $db->error());

	while ($cur_row = $db->fetch_assoc($result))
	{
		$classesArray[] = array(
			'ClassID' => $cur_row['ClassID'],
			'Name' => $cur_row['Name'],
			'colour' => $cur_row['colour'],
			'image' => $cur_row['image']
			);
	}
	return $classesArray;
}
// -------------------------------------------------------------
function getRaiderNamesArray()
{
	global $db;
	$raiderNamesArray = array();
	$result = $db->query('
	select
		MemberID, Name
	from
		dmzdkp_members
		', true) or error('Unable to fetch member data', __FILE__, __LINE__, $db->error());

	while ($cur_row = $db->fetch_assoc($result))
	{
		$raiderNamesArray[$cur_row['MemberID']] = $cur_row['Name'];
	}
	return $raiderNamesArray;
}
// -------------------------------------------------------------
function displayDKPLog($limit)
{
	global $db, $pun_user;
	echo '<p style="font-weight: bold">Event log (GMT)</p>';
//	if (!isset($filter) || $filter = '')
//		$filter = "class like 'dkpL%'";
	$raiderNamesArray = getRaiderNamesArray();
	$classesArray = getClassesArray('ClassID');
	$rolesArray = getRolesArray('RoleID');
	$itemArray = array();
	$result = $db->query('
			SELECT
				*
			FROM
				clandmz_site_content
			WHERE
				class like "dkpL%"
			ORDER BY start_date DESC
			LIMIT '.$limit.'
		', true) or error('Unable to fetch log data', __FILE__, __LINE__, $db->error());
	while ($cur_row = $db->fetch_assoc($result))
	{
		$itemArray[] = array(
			'id' => $cur_row['id'],
			'class' => $cur_row['class'],
			'code' => $cur_row['code'],
			'decode' => $cur_row['decode'],
			'value' => $cur_row['value'],
			'reference' => $cur_row['reference'],
			'title' => $cur_row['title'],
			'author' => $cur_row['author'],
			'authoriser' => $cur_row['authoriser'],
			'start_date' => $cur_row['start_date'],
			'end_date' => $cur_row['end_date']
			);
	}
	foreach ($itemArray as $item)
	{
		switch ($item['class']) {
//	function addLogEntry($class, $code, $decode, $value, $reference, $title, $author, $authoriser, $start_date, $end_date)
//	addLogEntry('dkpLAdRaid', '', $date, $dungeon, $tier, '', $pun_user['username'], '', 'now', '');
		case "dkpLAdRaid": // Add raid
			$dungeonsArray = getDungeonsArray();
			echo date('D j M y H:i',$item[start_date]).' - Raid added: <img src="./site/dkp/images/instances/tier'.$item[reference].'.gif" title="Tier '.$item[reference].'"/>&nbsp;'.$dungeonsArray[$item[value]][dungeonName].' on '.date('D j M y',$item[decode]).' by '.$item[author].'<br />';
			break;
		case "dkpLAddRdr": // Add raider
			echo date('D j M y H:i',$item[start_date]).' - Raider added: <img src="'.$rolesArray[($item[reference])][image].'" height="15px" width="15px" title="'.$rolesArray[($item[reference])][RoleName].'" />&nbsp;<img src="./img/clandmz/'.$classesArray[($item[value])][image].'" height="15px" width="15px" title="'.$classesArray[($item[value])][Name].'" />&nbsp;&nbsp;<span style="color: #'.$classesArray[($item[value])][colour].'; font-weight: bold;">'.$item[decode].'</span> by '.$item[author].'<br />';
			// by '.pun_user[$item[author]].'
			break;
		case "dkpLRemRdr": // Remove raider
			$removedRaiders = explode(", ", $item[decode]);
			$plural = 's';
			$nameList = '';
			if (count($removedRaiders) == 1)
				$plural = '';
			echo date('D j M y H:i',$item[start_date]).' - Raider'.$plural.' deactivated: ';
			foreach ($removedRaiders as $raider)
			{
				$nameList .= $raiderNamesArray[$raider].', ';
			}
			echo substr($nameList, 0, -2).' by '.$item[author].'<br />';
			break;
		case "dkpLActRdr": // Activate Raider
			$activatedRaiders = explode(", ", $item[decode]);
			$plural = 's';
			$nameList = '';
			if (count($activatedRaiders) == 1)
				$plural = '';
			echo date('D j M y H:i',$item[start_date]).' - Raider'.$plural.' activated: ';
			foreach ($activatedRaiders as $raider)
			{
				$nameList .= $raiderNamesArray[$raider].', ';
			}
			echo substr($nameList, 0, -2).' by '.$item[author].'<br />';
			break;
		case "dkpLEdtRdr": //Edit Raider
			echo date('D j M y H:i',$item[start_date]).' - Raider updated: <img src="'.$rolesArray[($item[authoriser]-1)][image].'" height="15px" width="15px" title="'.$rolesArray[($item[authoriser]-1)][RoleName].'" />&nbsp;<img src="./img/clandmz/'.$classesArray[($item[code])][image].'" height="15px" width="15px" title="'.$classesArray[($item[code])][Name].'" />&nbsp;&nbsp;<span style="color: #'.$classesArray[($item[code])][colour].'; font-weight: bold;">'.$item[title].'</span> => <img src="'.$rolesArray[($item[reference]-1)][image].'" height="15px" width="15px" title="'.$rolesArray[($item[reference]-1)][RoleName].'" />&nbsp;<img src="./img/clandmz/'.$classesArray[($item[value])][image].'" height="15px" width="15px" title="'.$classesArray[($item[value])][Name].'" />&nbsp;&nbsp;<span style="color: #'.$classesArray[($item[value])][colour].'; font-weight: bold;">'.$item[decode].'</span> by '.$item[author].'<br />';
			break;
		default:
			echo date('D j M y H:i', $item[start_date]).' - '.$item[decode].'<br />';
		}
	}
//	print_r($pun_user);
}
// -------------------------------------------------------------
function getItemColour($itemID)
{
	$itemColour = 'a335ee';
	if ($itemID == 32838 || $itemID == 32837 || $itemID == 34334)
	{
		$itemColour = 'ff8000';
	}
	return $itemColour;
}
// -------------------------------------------------------------
function getItemDisplay($gameItemID, $itemColour, $itemImage, $itemName, $itemValue)
{
	return '<a href="http://www.wowhead.com/?item='.$gameItemID.'" style="text-decoration: none; font-weight: bold; color: #'.$itemColour.'"><img src="./images/wow_icons/'.$itemImage.'.png" height="18px" width="18px"/> '.$itemName.'</a>&nbsp;<span style="font-size: 8px;">('.$itemValue.')</span>';
}
// -------------------------------------------------------------
function getRaiderDetail($raiderID)
{
	global $db;
//	$orderBy = 'ItemName';
	$raiderDetailArray = array();
	$result = $db->query('
		SELECT
			m.RaidID,
			m.MemberID,
			r.DungeonID,
			r.BonusValue,
			r.Date,
			r.wws_url,
			r.hidden,
			b.ItemID,
			b.MemberID as ItemMemberID,
			i.MobID,
			i.Name,
			i.iLevel,
			i.SlotID,
			i.gameItemID,
			i.image,
			v.ItemValue,
			rv.RaidValue
		FROM
			dmzdkp_raidmembers m,
			dmzdkp_raids r
			LEFT OUTER JOIN	dmzdkp_balance b
			ON b.RaidID = r.RaidID
			LEFT OUTER JOIN	dmzdkp_items i
			ON i.ItemID = b.ItemID
			LEFT OUTER JOIN	dmzdkp_itemValues v
			ON v.ItemID = b.ItemID
			LEFT OUTER JOIN	dmzdkp_raidValues rv
			ON rv.RaidID = r.RaidID
		WHERE
			m.MemberID = '.$raiderID.'
		AND	r.RaidID = m.RaidID
		ORDER BY Date DESC, DungeonID
	', true) or error('Unable to fetch raider data (getRaiderDetail)', __FILE__, __LINE__, $db->error());

	while ($cur_row = $db->fetch_assoc($result))
	{
		$raiderDetailArray[] = array(
			'RaidID' => $cur_row['RaidID'],
			'MemberID' => $cur_row['MemberID'],
			'DungeonID' => $cur_row['DungeonID'],
			'BonusValue' => $cur_row['BonusValue'],
			'Date' => $cur_row['Date'],
			'wws_url' => $cur_row['wws_url'],
			'hidden' => $cur_row['hidden'],
			'ItemID' => $cur_row['ItemID'],
			'ItemMemberID' => $cur_row['ItemMemberID'],
			'MobID' => $cur_row['MobID'],
			'Name' => $cur_row['Name'],
			'iLevel' => $cur_row['iLevel'],
			'SlotID' => $cur_row['SlotID'],
			'gameItemID' => $cur_row['gameItemID'],
			'image' => $cur_row['image'],
			'ItemValue' => $cur_row['ItemValue'],
			'RaidValue' => $cur_row['RaidValue'],
			);
	}
	return $raiderDetailArray;
}
// -------------------------------------------------------------
function getNumPreviousRaids($raidID)
{
	global $db;
	$numPreviousRaidsArray = array();
	$result = $db->query('
		SELECT
			m.name,
			(
					select
						count(raidID)
					from
						dmzdkp_raidmembers
					where
						MemberID = m.MemberID
					and	RaidID < '.$raidID.'
			) as NumRaids
		FROM
			dmzdkp_members m
		ORDER BY 1
	', true) or error('Unable to fetch raider data (getNumPreviousRaids)', __FILE__, __LINE__, $db->error());

	while ($cur_row = $db->fetch_assoc($result))
	{
		$numPreviousRaidsArray[$cur_row['name']] = $cur_row['NumRaids'];
	}
	return $numPreviousRaidsArray;
}
// -------------------------------------------------------------
function getTodaysRaidID()
{
	global $db;
	$todaysRaidID = 1;
	$result = $db->query('
		SELECT raid_id
		FROM dmzraid_raids
		WHERE date(raid_start) = date(curdate())
	', true) or error('Unable to fetch signup data', __FILE__, __LINE__, $db->error());

	while ($cur_row = $db->fetch_assoc($result))
	{
		$todaysRaidID = $cur_row['raid_id'];
	}
	return $todaysRaidID;
}
// -------------------------------------------------------------
function getSignedMembers($raidNinjaRaidID="481")
{
	if ($raidNinjaRaidID == "481")
		$raidNinjaRaidID = getTodaysRaidID();
	if (isset($_GET['rn']))
		$raidNinjaRaidID = $_GET['rn'];
	global $db;
	$signedMembersArray = array();
	$result = $db->query('
		SELECT
			c.name
		FROM
			dmzraid_character_raid r,
			dmzraid_characters c
		WHERE
			r.raid_id = '.$raidNinjaRaidID.'
		AND	r.signup_type = 2
		AND c.character_id = r.character_id
	', true) or error('Unable to fetch signup data', __FILE__, __LINE__, $db->error());

	while ($cur_row = $db->fetch_assoc($result))
	{
		$signedMembersArray[] = $cur_row['name'];
	}
	return $signedMembersArray;
}
// -------------------------------------------------------------
function getSlotInfo($wowheadSlotID)
{
	switch ($wowheadSlotID) {
		case "1": $slotString = '1,Head'; break;
		case "2": $slotString = '2,Neck'; break;
		case "3": $slotString = '3,Shoulder'; break;
		case "16": $slotString = '4,Back'; break;
		case "5": $slotString = '5,Chest'; break;
		case "9": $slotString = '6,Wrist'; break;
		case "10": $slotString = '7,Hand'; break;
		case "6": $slotString = '8,Waist'; break;
		case "7": $slotString = '9,Leg'; break;
		case "8": $slotString = '10,Feet'; break;
		case "11": $slotString = '11,Finger'; break;
		case "12": $slotString = '12,Trinket'; break;
		case "13": $slotString = '13,Weapon'; break;
		case "17": $slotString = '13,Weapon'; break;
		case "21": $slotString = '13,Weapon'; break;
		case "22": $slotString = '13,Weapon'; break;
		case "23": $slotString = '14,Off-hand'; break;
		case "28": $slotString = '14,Off-hand'; break;
		case "14": $slotString = '15,Shield'; break;
		case "15": $slotString = '16,Ranged'; break;
		default: $slotString = '1 ,Unknown - <b>DO NOT ADD ITEM</b>(S)';
	}
	return $slotString;
}
// -------------------------------------------------------------
function getItemDataArray($itemIDs)
{
	require_once('xmlparser.php');
	$itemData = array();
	$items = $itemIDs;
	$items = str_replace(";", ":", $items);
	$items = str_replace(",", ":", $items);
	if (empty($items))
		return $itemData;
	$item_list = explode(":", $items);
	$i = 0;
	foreach($item_list as $item_id) {
		if(!is_numeric($item_id))
			$item_id = 22726;
		$request_url = "http://www.wowhead.com/item=$item_id&xml";
		$ch = curl_init();
		$timeout = 5;
		curl_setopt($ch, CURLOPT_URL, $request_url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');
		$data = curl_exec($ch);
		curl_close($ch);
		$parser = new XMLParser($data, 'raw', 1);
		$tree = $parser->getTree();
		$otherdata = array();
		$jsonarray = explode(",", $tree[WOWHEAD][ITEM][JSON][VALUE]);
		foreach ($jsonarray as $value) {
			if(strstr($value, '"slot":'))
				$otherdata[slot] = substr($value, 7);
			if(strstr($value, '"n":')) {
				$tempdata = substr($value, 4);
				$otherdata[mob] = trim($tempdata, '"');
				$otherdata[mob] = str_replace("\\", "", $otherdata[mob]);
			}

		}
		
		if(empty($otherdata[mob])) { $otherdata[mob] = "Dunno"; }
		if(empty($tree[WOWHEAD][ITEM][NAME][VALUE])) { $tree[WOWHEAD][ITEM][NAME][VALUE] = "Invalid Item ID."; }
		$itemData[$i]['name'] = $tree[WOWHEAD][ITEM][NAME][VALUE];
		$itemData[$i]['id'] = $tree[WOWHEAD][ITEM][ATTRIBUTES][ID];
		$itemData[$i]['icon'] = $tree[WOWHEAD][ITEM][ICON][VALUE];
		$itemData[$i]['quality'] = $tree[WOWHEAD][ITEM][QUALITY][VALUE];
		$itemData[$i]['level'] = $tree[WOWHEAD][ITEM][LEVEL][VALUE];
		$itemData[$i]['subclass'] = $tree[WOWHEAD][ITEM][subclass][VALUE];
		$itemData[$i]['slot'] = $otherdata[slot];
		$itemData[$i]['mob'] = $otherdata[mob];
		$i++;
	}
	return $itemData;
}
// -------------------------------------------------------------
function getTierDrops($tier)
{
	global $db;
	$tierItemArray = getTierItemArray($tier);
	foreach ($tierItemArray as $itemID) {
		$itemIDList .= $itemID.',';
	}
	$itemIDList = substr($itemIDList, 0, -1);
	$tierDropArray = array();
	$result = $db->query('
		SELECT
			b.MemberID,
			b.ItemID,
			t.Name,
			t.SlotID,
			t.gameItemID,
			t.image
		FROM
			dmzdkp_balance b,
			dmzdkp_items t
		WHERE
			b.ItemID IN (SELECT
							i.ItemID
						FROM
							dmzdkp_items i
						WHERE
							i.gameItemID IN ('.$itemIDList.')
						AND	i.iLevel > 0)
		AND
			b.ItemID = t.ItemID
		ORDER BY t.gameItemID
	', true) or error('Unable to fetch tier item data', __FILE__, __LINE__, $db->error());

	while ($cur_row = $db->fetch_assoc($result))
	{
		$tierDropArray[] = array(
			'MemberID' => $cur_row['MemberID'],
			'ItemID' => $cur_row['ItemID'],
			'Name' => $cur_row['Name'],
			'SlotID' => $cur_row['SlotID'],
			'gameItemID' => $cur_row['gameItemID'],
			'image' => $cur_row['image'],
			);
	}
	return $tierDropArray;
}
// ------------------------------------------------------------- 
function getRaiderDisplayName($id, $raiderDisplayArray, $nameFormat) {
// Receives a membership ID that relates to the table dmzdkp_members, an array of member display data from
// dkpfunctions.php > getRaiderDisplayArray(), an optional line ending (often '<br />') , optional text for a title,
// and an option font weight.
// Formats this into a DKP system member link formatted for display and echos this result.
	$roleImage = '';
	$classImage = '';
	if ($nameFormat['roleImage'])
		$roleImage = '<img src="'.$raiderDisplayArray[$id]['roleImage'].'" height="15px" width="15px" title="'.$raiderDisplayArray[$id]['roleName'].'" />&nbsp;';
	if ($nameFormat['classImage'])
		$classImage = '<img src="./img/clandmz/'.$raiderDisplayArray[$id]['classImage'].'" height="15px" width="15px" title="'.$raiderDisplayArray[$id]['className'].'" />&nbsp;&nbsp;';
	$link = '<a href="index.php?page=dkptables&m='.$id.'" title="'.$nameFormat['titleText'].'" style="color: #'.$raiderDisplayArray[$id]['classColour'].'; font-weight: '.$nameFormat['fontWeight'].'; text-decoration: none">';
	
	$raiderDisplayName = $roleImage.$classImage.$link.$raiderDisplayArray[$id]['userName'].'</a>'.$nameFormat['lineEnd'];
	return $raiderDisplayName;
}// -------------------------------------------------------------
/* -------------------------------------------------------------
------------------------------------------------------------- */
?>
